import { useLocation } from "react-router-dom"
 
import  { useState, useEffect } from 'react';
import { useDispatch } from 'react-redux';
// import { routesList } from './routersList'
// import MarketplaceList from "@/operate/marketplace/marketplace-list" // 运营中心-市场管理-市场列表
// import MarketplaceGrid from "@/operate/marketplace/marketplace-grid" // 运营中心-市场管理-网格列表

// import MerchantList from "@/operate/merchant/merchant-list" // 运营中心-商户管理-商户列表
// import Merchantcollection from "@/operate/merchant/merchant-collection" // 运营中心-商户管理-商户收款
// import MerchantEmployee from "@/operate/merchant/merchant-employee" // 运营中心-商户管理-员工列表

// import ManageList from "@/member/manage/manage-list" // 会员中心-会员管理-会员列表

// import CrowdLabel from "@/member/see-clearly/crowd-label" // 会员中心-会员洞察-人群标签
// import StoreRenewal from "@/member/see-clearly/store-renewal" // 会员中心-会员洞察-门店拉新
// import ClearlyTrait from "@/member/see-clearly/clearly-trait" // 会员中心-会员洞察-会员特征

import { Suspense, lazy } from "react"
// import Loadable from "react-loadable"
// 包装 React.lazy函数
function LazyWrapper(path) {
  // console.log('path',menuItem.element)
  const Component = lazy(() => import(/* @vite-ignore */ `../views${path}`))
  return (
    <Suspense fallback={<div>loading</div>}>
      <Component />
    </Suspense>
  );
}
import { LhuserInfoWxLogin } from "@/apis/index"
// 收集所有的叶子节点，生成路由信息
function genRouter(menuConfig) {
  menuConfig.forEach(menuItem => {
    if (!menuItem.children || !menuItem.children.length) {
        menuItem.element = LazyWrapper(menuItem.path)
    } else genRouter(menuItem.children)
  })
  return menuConfig
}
export function useRoutesData() {
  const [route, setRoutes] = useState([]);
  // 初始化选中的导航
  const location = useLocation();
  const dispatch = useDispatch()
  // 获取当前路由地址
  const pathname = location.pathname.split('/')[1]
  useEffect(() => {
    dispatch((dis) => LhuserInfoWxLogin().then(data => {
      data.data.map(res => {
        if(`/${pathname}` == res.path) {
          // console.log('resresresres',res)
          dis({ type: 'SelectNav', value: res })
        }
      })
      return setRoutes(genRouter(data.data))
    }))
  }, []);
  // console.log('routerouterouteroute',route)

  // console.log('pathname',pathname)

  return route;
}
// console.log('useRoutesData',useRoutesData)


